<template>
  <div class="notification-center">
    <div class="notification-icon" @click="toggleNotifications">
      <i class="fas fa-bell"></i>
      <span v-if="unreadCount" class="badge">{{ unreadCount }}</span>
    </div>
    <div v-if="isOpen" class="notification-panel">
      <div class="panel-header">
        <h3>通知中心</h3>
        <button @click="markAllAsRead" :disabled="!notifications.length">
          全部已读
        </button>
      </div>
      <div class="notification-list" v-if="notifications.length">
        <div
          v-for="notification in notifications"
          :key="notification.id"
          class="notification-item"
          :class="{ unread: !notification.read }"
          @click="handleNotificationClick(notification)"
        >
          <div class="notification-content">
            <p class="title">{{ notification.title }}</p>
            <p class="message">{{ notification.message }}</p>
            <span class="time">{{ formatTime(notification.createdAt) }}</span>
          </div>
        </div>
      </div>
      <div v-else class="empty-state">
        暂无通知
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useNotificationStore } from '../../stores/notification.store'
import { formatDistanceToNow } from 'date-fns'
import { zhCN } from 'date-fns/locale'

const notificationStore = useNotificationStore()
const isOpen = ref(false)

const notifications = computed(() => notificationStore.notifications)
const unreadCount = computed(() => notificationStore.unreadCount)

const toggleNotifications = () => {
  isOpen.value = !isOpen.value
}

const markAllAsRead = async () => {
  await notificationStore.markAllAsRead()
}

const handleNotificationClick = async (notification: any) => {
  if (!notification.read) {
    await notificationStore.markAsRead(notification.id)
  }
  // 处理通知点击事件，例如导航到相关页面
}

const formatTime = (date: string | Date) => {
  return formatDistanceToNow(new Date(date), { addSuffix: true, locale: zhCN })
}
</script>

<style scoped lang="scss">
.notification-center {
  position: relative;

  .notification-icon {
    position: relative;
    cursor: pointer;
    padding: 8px;

    i {
      font-size: 1.2rem;
      color: #666;
    }

    .badge {
      position: absolute;
      top: 0;
      right: 0;
      background-color: #ff4d4f;
      color: white;
      border-radius: 10px;
      padding: 0 6px;
      font-size: 12px;
      min-width: 16px;
      height: 16px;
      line-height: 16px;
      text-align: center;
    }
  }

  .notification-panel {
    position: absolute;
    top: 100%;
    right: 0;
    width: 300px;
    background: white;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    z-index: 1000;

    .panel-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 16px;
      border-bottom: 1px solid #f0f0f0;

      h3 {
        margin: 0;
        font-size: 16px;
        color: #333;
      }

      button {
        border: none;
        background: none;
        color: #1890ff;
        cursor: pointer;
        font-size: 14px;

        &:disabled {
          color: #d9d9d9;
          cursor: not-allowed;
        }
      }
    }

    .notification-list {
      max-height: 400px;
      overflow-y: auto;

      .notification-item {
        padding: 12px 16px;
        cursor: pointer;
        transition: background-color 0.3s;

        &:hover {
          background-color: #f5f5f5;
        }

        &.unread {
          background-color: #e6f7ff;

          &:hover {
            background-color: #bae7ff;
          }
        }

        .notification-content {
          .title {
            margin: 0 0 4px;
            font-size: 14px;
            color: #333;
          }

          .message {
            margin: 0 0 4px;
            font-size: 12px;
            color: #666;
          }

          .time {
            font-size: 12px;
            color: #999;
          }
        }
      }
    }

    .empty-state {
      padding: 32px 16px;
      text-align: center;
      color: #999;
      font-size: 14px;
    }
  }
}
</style>